<?php
function __the_uclient_option_panel($ctlrs, $group, $options) {
	if ($group != 'uclient') {
		return $ctlrs;
	}
	$opts = array ();
	$type = $options ['uc_connect'];
	$type = empty ( $type ) ? 1 : 'mysql';
	$checked [$type] = ' checked="checked"';
	$opts [] = '<script type="text/javascript">
					function showmysql(show){var mysqldiv = $("#uc_mysql_option");show?mysqldiv.show():mysqldiv.hide();}
				</script>';
	$opts [] = '<div style="padding:5px 0;margin:0 0 10px;;border:1px solid #999;">';
	$opts [] = '<h2 style="padding-left:20px;">通信设置</h2>';
	$opts [] = '<div class="control-group">
	       			<label class="control-label">通信方式(UC_CONNECT)</label>
	           		<div class="controls">
	           			<label class="radio inline"><input type="radio" onclick="showmysql(1);" value="mysql" name="opt[uclient][uc_connect]"' . $checked ['mysql'] . '/>mysql</label>
						<label class="radio inline"><input type="radio" onclick="showmysql(0);" value="" name="opt[uclient][uc_connect]"' . $checked [1] . '/>fscoketopen</label>
						<br/>	
	           			<span class="help-inline refval">注:连接 UCenter的方式,mysql是直接连接的数据库, 为了效率, 建议采用 mysql.</span>	              			
	           		</div>
	       		</div>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_key" class="control-label">通信密钥(UC_KEY)</label>
	           		<div class="controls">
	           			<input class="span4" type="text" validate="{validate:{required:true,messages:{required:\'通信密钥不能为空.\'} }}" value="' . $options ['uc_key'] . '" name="opt[uclient][uc_key]" id="uc_key"/>	
	           			<span class="help-inline">与 UCenter的通信密钥, 要与 UCenter 的设置保持一致.</span>	              			
	           		</div>
	       		</div>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_api" class="control-label">通信地址(UC_API)</label>
	           		<div class="controls">
	           			<input class="span4" type="text" validate="{validate:{required:true,url:true,messages:{required:\'通信地址不能为空.\',url:\'请输入正确的UCenter URL.\'} }}" value="' . $options ['uc_api'] . '" name="opt[uclient][uc_api]" id="uc_api"/>	
	           			<span class="help-inline">UCenter的 URL地址, 在调用头像时依赖此地址.</span>	              			
	           		</div>
	       		</div>';
	
	$opts [] = '<div class="control-group">
	       			<label for="uc_ip" class="control-label">通信IP(UC_IP)</label>
	           		<div class="controls">
	           			<input class="span4" type="text"  value="' . $options ['uc_ip'] . '" name="opt[uclient][uc_ip]" id="uc_ip"/>	
	           			<span class="help-inline">UCenter的 IP,当通信方式为非 mysql方式且<br/>通信有问题时,请设置此值.</span>	              			
	           		</div>
	       		</div>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_appid" class="control-label">应用ID(UC_APPID)</label>
	           		<div class="controls">
	           			<input class="span4" type="text" validate="{validate:{required:true,digits:true,messages:{required:\'应用ID不能为空.\',digits:\'应用ID只能为数字.\'} }}"  value="' . $options ['uc_appid'] . '" name="opt[uclient][uc_appid]" id="uc_appid"/>	
	           			<span class="help-inline">当前应用的在UCenter中注册的ID.</span>	              			
	           		</div>
	       		</div>';
	
	$opts [] = '</div><div id="uc_mysql_option" style="padding:5px 0;margin:0 0 10px;;border:1px solid #999;display:' . ($type === 1 ? 'none' : 'block') . '">';
	$opts [] = '<h2 style="padding-left:20px;">UCenter数据库设置</h2>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_dbhost" class="control-label">UC数据库主机(UC_DBHOST)</label>
	           		<div class="controls">
	           			<input class="span4" type="text" validate="{validate:{required:true,messages:{required:\'UC数据库主机不能为空.\'} }}"  value="' . $options ['uc_dbhost'] . '" name="opt[uclient][uc_dbhost]" id="uc_dbhost"/>	
	           			<span class="help-inline">UCenter 数据库主机</span>
	           		</div>
	       		</div>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_dbuser" class="control-label">UC数据库用户名(UC_DBUSER)</label>
	           		<div class="controls">
	           			<input class="span4" type="text" validate="{validate:{required:true,messages:{required:\'UCenter 数据库用户名不能为空.\'} }}"  value="' . $options ['uc_dbuser'] . '" name="opt[uclient][uc_dbuser]" id="uc_dbuser"/>	
	           			<span class="help-inline">UCenter 数据库用户名</span>
	           		</div>
	       		</div>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_dbpw" class="control-label">UC数据库密码(UC_DBPW)</label>
	           		<div class="controls">
	           			<input class="span4" type="password" validate="{validate:{required:true,messages:{required:\'UCenter 数据库密码不能为空.\'} }}"  value="' . $options ['uc_dbpw'] . '" name="opt[uclient][uc_dbpw]" id="uc_dbpw"/>	
	           			<span class="help-inline">UCenter 数据库密码</span>
	           		</div>
	       		</div>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_dbname" class="control-label">UC数据库名称(UC_DBNAME)</label>
	           		<div class="controls">
	           			<input class="span4" type="text" validate="{validate:{required:true,messages:{required:\'UCenter 数据库名称不能为空.\'} }}"  value="' . $options ['uc_dbname'] . '" name="opt[uclient][uc_dbname]" id="uc_dbname"/>	
	           			<span class="help-inline">UCenter数据库名称</span>
	           		</div>
	       		</div>';
	$opts [] = '<div class="control-group">
	       			<label for="uc_dbtablepre" class="control-label">UC数据表前缀(UC_DBTABLEPRE)</label>
	           		<div class="controls">
	           			<input class="span4" type="text" value="' . $options ['uc_dbtablepre'] . '" name="opt[uclient][uc_dbtablepre]" id="uc_dbtablepre"/>	
	           			<span class="help-inline">UCenter 数据库表前缀</span>
	           		</div>
	       		</div>';
	
	$opts [] = '</div>';
	$ctlrs .= implode ( "\n", $opts );
	return $ctlrs;
}
bind ( 'show_option_control', '__the_uclient_option_panel', 10, 3 );

//保存配置
bind ( 'on_save_option_uclient', 'on_save_option_uclient' );
function on_save_option_uclient($options) {
	$opary = array ();
	$options = $options ['uclient'];
	foreach ( $options as $key => $value ) {
		if ($key == 'uc_api') {
			$value = trim ( $value, '/' );
		} else if ($key == 'uc_dbpw' && ! empty ( $value )) {
			$value = ntzauthcode ( $value, 'ENCODE' );
		}
		$opary [] = "$key = $value";
	}
	$len = @file_put_contents ( APPDATA_PATH . '/uclient/option.ini', implode ( "\n", $opary ) );
	if ($len > 0) {
		return true;
	} else {
		return '无法保存文件:' . APPDATA_PATH . '/uclient/option.ini';
	}
}
//读取缓存配置 
bind ( 'get_option_uclient', 'get_option_uclient_option' );
function get_option_uclient_option($options = array()) {	
	if (file_exists ( APPDATA_PATH . '/uclient/option.ini' )) {
		$options = parse_ini_file ( APPDATA_PATH . '/uclient/option.ini' );
	}
	if (isset ( $options ['uc_dbpw'] )) {
		$options ['uc_dbpw'] = ntzauthcode ( $options ['uc_dbpw'], 'DECODE' );
	}
	if ($options ['uc_dbpw'] === false) {
		$options ['uc_dbpw'] = '';
	}
	return $options;
}